var express = require('express')
var fs = require('fs');
var path = require('path');
var router = express.Router()
var jwt = require('jsonwebtoken')

router.get('/list', (req, res) => {
  var { page, count } = req.query
  var filepath = path.join(__dirname, '../user.json')
  var arr = fs.readFileSync(filepath)
  arr = arr.toString() ? JSON.parse(arr) : []
  var newarr = arr.slice((page - 1) * count, page * count);
  res.send({
    code: 200,
    msg: '请求成功',
    data: newarr
  })
})
router.get('/search', (req, res) => {
  var { searchkey } = req.query;
  var filepath = path.join(__dirname, '../user.json');
  var arr = fs.readFileSync(filepath);
  arr = arr.toString() ? JSON.parse(arr) : []
  var newarr = arr.filter((item) => {
    return new RegExp(searchkey).test(item.name)
  })
  res.send({ code: 200, msg: '查询成功!', data: newarr })
})
router.post('/login', (req, res) => {
  var { phone, pass } = req.body;

  var filepath = path.join(__dirname, '../user.json');
  var arr = fs.readFileSync(filepath);
  arr = arr.toString() ? JSON.parse(arr) : []
  var i = arr.findIndex((item, index) => {
    return item.phone == phone && item.pass == pass;
  })

  if (i != -1) {
    var token = jwt.sign({phone},"1234",{algorithm:'HS256',expiresIn:60})
    res.send({ code: 200, msg: '登陆成功!', userinfo: arr[i],token});
  } else {
    res.send({ code: 400, msg: '登陆失败!' });
  }
})

router.post('/register', (req, res) => {
  var { phone } = req.body
  var filepath = path.join(__dirname, '../user.json')
  var arr = fs.readFileSync(filepath);
  arr = arr.toString() ? JSON.parse(arr) : []
  var i = arr.findIndex((item) => {
    return item.phone == phone;
  })
  if (i !== -1) {
    res.send({ code: 400, msg: '注册失败' })
  } else {
    arr.push({ ...req.body, ingroup: '编辑组', isadmin: true, status: true })
    fs.writeFileSync(filepath, JSON.stringify(arr), 'utf8');
    res.send({
      code: 200, msg: '注册成功'
    })
  }
})

router.post('/delete', (req, res) => {
  var { phone } = req.body
  var filepath = path.join(__dirname, '../user.json')
  var arr = fs.readFileSync(filepath);
  arr = arr.toString() ? JSON.parse(arr) : []
  var i = arr.findIndex((item) => {
    return item.phone = phone
  })
  if (i != -1) { 
    arr.splice(i, 1);
    fs.writeFileSync(filepath, JSON.stringify(arr), 'utf8');
    var newarr = arr.slice(0, 2);
    res.send({ code: 200, msg: '删除成功!', data: newarr })
  } else {
    res.send({ code: 400, msg: '删除失败!' })
  }
})

router.post('/push', (req, res) => {
  var filepath = path.join(__dirname, '../user.json')
  var arr = fs.readFileSync(filepath);
  arr = arr.toString() ? JSON.parse(arr) : []
  var { phone } = req.body
  var i = arr.findIndex((item) => {
    return item.phone == phone
  })
  if (i != -1) {
    res.send({ code: 400, msg: '用户已存在' })
  } else {
    arr.push({ ...req.body, ingroup: '编辑组', isadmin: true, status: true })
    fs.writeFileSync(filepath, JSON.stringify(arr), 'utf8');
    res.send({code:200,msg:'用户添加成功'})
  }
})
module.exports = router